package com.neu.cloudfactory.controler;

import com.neu.cloudfactory.bean.Equipement.Equipmenttype;
import com.neu.cloudfactory.bean.Factory.Factory;
import com.neu.cloudfactory.bean.Product.Producttype;
import com.neu.cloudfactory.bean.User.User;
import com.neu.cloudfactory.repository.EquipmentRepository;
import com.neu.cloudfactory.repository.FactoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FactoryControler {
    @Autowired
    FactoryRepository factoryRepository;
    @Autowired
    EquipmentRepository equipmentRepository;

    @RequestMapping("/factory/findAll/{page}/{size}")
    public Page<Factory> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return factoryRepository.findAll(pageable);
    }

    @RequestMapping("/factory/changeStatus/{id}/{statusid}")
    public Factory changeStaus(@PathVariable("id")Integer id, @PathVariable("statusid") Integer statusid){
        Factory factory = factoryRepository.findById(id).get();
        if(factory!=null){
            switch (statusid){
                case 1:
                    factory.setStatusid(2);
                    break;
                case 2:
                    factory.setStatusid(1);
                    break;
                default:
                    break;
            }
            factoryRepository.save(factory);
            return factory;
        }
        return null;
    }

    @RequestMapping("/factory/findById/{id}")
    public Factory findById(@PathVariable("id") Integer id){
        return factoryRepository.findById(id).get();
    }

    @RequestMapping("/factory/update")
    public String update(@RequestBody Factory factory){
        if(factory.getId()!=null){
            factoryRepository.save(factory);
            return "success";
        }else{
            return "failed";
        }
    }

    @RequestMapping("/factory/search/{page}/{size}/{input}")
    public Page<Factory> search(@PathVariable("input") String input, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return factoryRepository.findAllByIdOrNameContainingOrDescriptionContainingOrManagerid(id,input,input,id,pageable);
    }

    @RequestMapping("/factory/findByManagerId/{id}")
    public Factory findByManagerId(@PathVariable("id") Integer id){
        return factoryRepository.findByManagerid(id);
    }

    @RequestMapping("/checkFactoryname/{factoryname}")
    public int checkFactoryname(@PathVariable("factoryname") String factoryname){
        if(factoryRepository.existsByName(factoryname)){
            return 1;
        }else {
            return 0;
        }
    }
}
